//
//  CustomerOrderVC.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/6/12.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "CustomerOrderVC.h"
#import "MBProgressHUD+MJ.h"
#import "MJRefresh.h"
#import "CustomerCell.h"
#import "CustomerOrderDetailVc.h"
#import "OrderDM.h"
#import "NodataCell.h"




@interface CustomerOrderVC ()

@property (nonatomic,strong)OrderDM *dm;
@property (nonatomic,strong)NSArray *dataArray;
@property (nonatomic,strong)NodataCell *nodataCell;

@property (weak, nonatomic) IBOutlet UITableView *tableView;


@end

@implementation CustomerOrderVC
@synthesize dm,dataArray,nodataCell;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
    [self refreshDataWithCache:NO];
   
}
-(void)initView
{
    [self.titleView setMianTitle:@"我的订单"];
    self.titleView.doneBtn.hidden = YES;
    dm = [[OrderDM alloc]init];
    
    
    [self.tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(refreshDataWithCache:)];
    [self.tableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
}

- (void)loadMore{
    [super loadMore];
    
    __weak typeof(self) bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        ResulteMod *mod = [dm loadMoreData:0];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
            if (mod.resltState == ResultNetError) {//网络出错
                
                
            }else if (mod.resltState == ResultFail){//服务器返回的code出错
                
                
            }else{
                
                NSMutableArray *array = [[NSMutableArray alloc]initWithArray:bself.dataArray];
                [array addObjectsFromArray:mod.dataMod];
                bself.dataArray = [[NSArray alloc]initWithArray:array];
                
            }
            
            [bself.tableView reloadData];
            
            [bself.tableView.footer endRefreshing];
            
            
        });
    });
}



-(void)refreshDataWithCache:(BOOL)cache{
    
    
    [self.nodataCell setState:nodataCellStateLoading];
    cache = NO;
    
    __weak typeof(self) bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        ResulteMod *mod =[dm refreshData:0 WithCache:cache];
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
            dataArray = mod.dataMod;
            
//            int count = 0;
            
            if (mod.resltState == ResultNetError) {//网络出错
                
                [bself.nodataCell setState:nodataCellStateNetError];
                
            }else if(mod.resltState == ResultFail){//服务器返回的code出错
                
                [bself.nodataCell setCodeErrorTip:mod.resultCode];
                
            }else{//返回正常
                
                if(bself.dataArray.count==0){//没数据
                    
//                    count=0;
                    
                    [bself.nodataCell setState:nodataCellStateNodata];
                    
                }else{//有数据
                    
//                     count = [abstractResponseDic[@"orderCount"] intValue];
                }
                
            }
            [bself.tableView reloadData];
            
            [bself.tableView.header endRefreshing];
        });
    });
    
}

#pragma tableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        if (dataArray&&dataArray.count>0) {
            [self.tableView setScrollEnabled:YES];
            return self.dataArray.count;
        }else{
            [self.tableView setContentOffset:CGPointMake(0, 0)];
            [self.tableView setScrollEnabled:NO];
            return 1;
        }
}
                   
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (dataArray.count==0) {
            if(!self.nodataCell){
                self.nodataCell = [[NodataCell alloc] initWithFrame:self.tableView.frame];
                [self.nodataCell setState:nodataCellStateLoading];
                [[self.nodataCell refreshBtn]setTag:56];
                [self.nodataCell.refreshBtn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
            }
            return self.nodataCell;
        }
        
        
        
        CustomerCell *cell = [CustomerCell cellWithTableView:tableView];
        cell.lookBtn.tag = indexPath.row;
        [cell.lookBtn addTarget:self action:@selector(lookBtnOnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [cell setData:[dataArray objectAtIndex:indexPath.row]];
        
        return cell;
    }
                   
                   
                   
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
    {
        if (dataArray&&dataArray.count>0) {
            
            return [CustomerCell getHight:nil withTableView:tableView];
        }
        
        return self.tableView.frame.size.height;
        
    }
                   
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (dataArray.count>0) {
            
            CustomerOrderDetailVc *vc = [[CustomerOrderDetailVc alloc]init];
            OrderData *data = [dataArray objectAtIndex:indexPath.row];
            vc.order = data;
            vc.hidesBottomBarWhenPushed = YES;
            
            [self.navigationController
             pushViewController:vc animated:YES];
        }
    }

-(void)lookBtnOnClick:(UIButton *)btn
{
    if (dataArray.count>0) {
        
        CustomerOrderDetailVc *vc = [[CustomerOrderDetailVc alloc]init];
        OrderData *data = [dataArray objectAtIndex:btn.tag];
        vc.order = data;
        [self.navigationController
         pushViewController:vc animated:YES];
    }

}
-(void)btnOnClick:(UIButton *)sender{
    
    
    if(sender.tag==56){
        [self refreshDataWithCache:NO];
    }
}

@end
